﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SmallJob.Model;
using SmallShop.Services.Interfaces;

namespace BackEnd.Controllers
{
    [Authorize]
    public class FeatureController : Controller
    {
        //
        // GET: /Feature/

        private readonly IFeatureService _FeatureService;
        public FeatureController(IFeatureService FeatureService)
        {
            _FeatureService = FeatureService;
        }
        public ActionResult Index()
        {
            return View(_FeatureService.All().Where(f=>!f.IsDeleted).ToList());
        }
        public ActionResult CheckTitleIsExist(String title)
        {
            return Json(_FeatureService.IsExisted(title));
        }
        public ActionResult Save(String title)
        {
            if (_FeatureService.IsExisted(title))
                return RedirectToAction("index","feature");
            else
            {
                var f = new Feature()
                {
                    Title = title,
                    IsDeleted = false,
                    CreatedDate = DateTime.Now,
                    Order = _FeatureService.Count() + 1
                };
                _FeatureService.Add(f);
                return RedirectToAction("index", "feature");
            }
        }
        public ActionResult SaveOrder(List<int> order)
        {
            for (int i = 0; i < order.Count; i++)
            {
                var f = _FeatureService.GetById(order[i]);
                f.Order = i + 1;
                _FeatureService.Update(f);
            }
            return RedirectToAction("index", "feature");
        }
        public ActionResult GetTitle(int Id) {
            return Json(_FeatureService.GetById(Id).Title);
        }
        public ActionResult AddFeatureItem(int fID,int itemID) {
            if (!_FeatureService.IsFeatureItemExist(fID, itemID)) {
                var fi = new FeatureItem()
                {
                    FeatureID = fID,
                    ProvideID = itemID
                };
                _FeatureService.AddFeatureItem(fi);
                return Json(fi);
            }
            return Json(false);
        }
        public ActionResult GetFeatureItem(int id) {
            return Json(_FeatureService.GetFeatureItems(id));
        }
        public ActionResult RemoveFeatureItem(int id) {
            _FeatureService.RemoveFeatureItem(id);
            return Json(true);
        }
        public ActionResult DeleteFeature(int id) {
            var f = _FeatureService.GetById(id);
            f.IsDeleted = true;
            _FeatureService.Update(f);
            return Json(true);
        }
        public ActionResult GetFeatureTitle(int id) {
            return Json(_FeatureService.GetById(id).Title);
        }
        public ActionResult Rename(int fID,string fName) {
            var f = _FeatureService.GetById(fID);
            f.Title = fName;
            _FeatureService.Update(f);
            return RedirectToAction("index","feature");
        }
    }
}
